//
//  SetFocus.m
//  Vesstack_a
//
//  Created by 陈德锋 on 16/3/9.
//  Copyright © 2016年 vesstack. All rights reserved.
//

#import "Setting.h"
#import "CleanCacheCellTableViewCell.h"

@interface Setting ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate, UIActionSheetDelegate >
@property (nonatomic, strong) CleanCacheCellTableViewCell *newcell;

@end
@implementation Setting


-(void) viewDidLoad{
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
     self.view.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    CGFloat fileSize = [self folderSizeAtPath:cachPath];
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *cachelable = [NSString stringWithFormat:@"%.2fMB",fileSize];
        _newcell.cache.text =cachelable;
        
    });
    
    //消除底部多余tableview
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    v.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:v];
    
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section ==0) {
        return 1;
    }else if (section ==1)
    {
        return 2;
    }else{
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text =@"账号管理";
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;

    }
    else if (indexPath.section == 1) {
        if (indexPath.row ==0) {
            UITableViewCell *cell = [[UITableViewCell alloc]init];
            cell.textLabel.text =@"修改密码";
            cell.textLabel.font = [UIFont systemFontOfSize:16.0];
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
        else
        {
            _newcell = [CleanCacheCellTableViewCell CleanCacheCellWithTableView:tableView];
            return _newcell;
           

        }
      
    }
    else {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text =@"注销账号";
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;


    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==1) {
        if (indexPath.row ==1) {
            
            UIAlertView *myAlertView;
            myAlertView = [[UIAlertView alloc]initWithTitle:@"清理完成" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [myAlertView show];
            [self myClearCacheAction];
            _newcell.cache.text =@"0.00 MB";
        }
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   
        return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    return 45;
}

#pragma mark === 暂时不用清除缓存=====
-(void)myClearCacheAction{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       NSLog(@"files :%lu",(unsigned long)[files count]);
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
}

-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
    //获取缓存大小。。
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    CGFloat fileSize = [self folderSizeAtPath:cachPath];
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%@",[NSString stringWithFormat:@"清理之后缓存%.2fMB",fileSize]);
        
    });
    
}
- (CGFloat)folderSizeAtPath:(NSString *)folderPath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) {
        return 0;
    }
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString *fileName = nil;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

- (long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
    
}



@end
